package com.detooa.detooa.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.detooa.common.annotation.Log;
import com.detooa.common.enums.BusinessType;
import com.detooa.detooa.domain.DeToOaConfig;
import com.detooa.detooa.service.IDeToOaConfigService;
import com.detooa.common.core.controller.BaseController;
import com.detooa.common.core.domain.AjaxResult;
import com.detooa.common.utils.poi.ExcelUtil;
import com.detooa.common.core.page.TableDataInfo;

/**
 * 推送配置信息Controller
 * 
 * @author detooa
 * @date 2024-01-17
 */
@Controller
@RequestMapping("/detooa/detooa")
public class DeToOaConfigController extends BaseController {
    private String prefix = "detooa/detooa";

    @Autowired
    private IDeToOaConfigService deToOaConfigService;

    @RequiresPermissions("detooa:detooa:view")
    @GetMapping()
    public String detooa()
    {
        return prefix + "/detooa";
    }

    /**
     * 查询推送配置信息列表
     */
    @RequiresPermissions("detooa:detooa:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DeToOaConfig deToOaConfig)
    {
        startPage();
        List<DeToOaConfig> list = deToOaConfigService.selectDeToOaConfigList(deToOaConfig);
        return getDataTable(list);
    }

    /**
     * 导出推送配置信息列表
     */
    @RequiresPermissions("detooa:detooa:export")
    @Log(title = "推送配置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DeToOaConfig deToOaConfig)
    {
        List<DeToOaConfig> list = deToOaConfigService.selectDeToOaConfigList(deToOaConfig);
        ExcelUtil<DeToOaConfig> util = new ExcelUtil<DeToOaConfig>(DeToOaConfig.class);
        return util.exportExcel(list, "推送配置信息数据");
    }

    /**
     * 新增推送配置信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存推送配置信息
     */
    @RequiresPermissions("detooa:detooa:add")
    @Log(title = "推送配置信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DeToOaConfig deToOaConfig)
    {
        return toAjax(deToOaConfigService.insertDeToOaConfig(deToOaConfig));
    }

    /**
     * 修改推送配置信息
     */
    @RequiresPermissions("detooa:detooa:edit")
    @GetMapping("/edit/{configId}")
    public String edit(@PathVariable("configId") Long configId, ModelMap mmap)
    {
        DeToOaConfig deToOaConfig = deToOaConfigService.selectDeToOaConfigByConfigId(configId);
        mmap.put("deToOaConfig", deToOaConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存推送配置信息
     */
    @RequiresPermissions("detooa:detooa:edit")
    @Log(title = "推送配置信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DeToOaConfig deToOaConfig)
    {
        return toAjax(deToOaConfigService.updateDeToOaConfig(deToOaConfig));
    }

    /**
     * 删除推送配置信息
     */
    @RequiresPermissions("detooa:detooa:remove")
    @Log(title = "推送配置信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(deToOaConfigService.deleteDeToOaConfigByConfigIds(ids));
    }

    /**
     * 执行推送OA
     */
    @RequiresPermissions("detooa:detooa:pushOa")
    @Log(title = "执行推送OA", businessType = BusinessType.OTHER)
    @GetMapping("/pushOa")
    @ResponseBody
    public AjaxResult pushOa(String configIds)
    {
        try {
            deToOaConfigService.pushOa(configIds);
            return success("推送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return error("推送失败");
        }
    }
}
